<template>
  <div class="form-div">
    <el-form
      :model="loginForm"
      status-icon
      ref="loginForm"
      label-width="100px"
      class="demo-loginForm"
    >
      <el-form-item label="User Name" prop="user_name">
        <el-input v-model="loginForm.user_name"></el-input>
      </el-form-item>
      <el-form-item label="Password" prop="password">
        <el-input
          type="password"
          v-model="loginForm.password"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm()">
          login
        </el-button>
        <el-button @click="resetForm('loginForm')">reset</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import { login } from "../api/user"
  export default {
    name: "Login",

    data() {
      return {
        loginForm: {
          password: "",
          user_name: "",
        },
      }
    },

    methods: {
      submitForm() {
        login(this.loginForm.user_name, this.loginForm.password)
          .then((res) => {
            if (res.status == 200) {
              var token = res.data.data.token
              this.$store.commit("setToken", token)
              //console.log(this.$store.getters.getToken)
              this.$router.push({ path: "/" })
            } else {
              this.$message.error("status not 200")
            }
          })
          .catch((error) => {
            console.log("submit form error: ", error)
            this.$message.error(
              "submit form error, Is username and password correct?"
            )
          })
      },
      resetForm(formName) {
        this.$refs[formName].resetFields()
      },
    },
  }
</script>

<style>
  .form-div {
    padding-left: 25em;
    padding-right: 25em;
    padding-top: 4em;
  }
</style>
